package net.youdi.gateway.context;

import lombok.Data;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.util.MultiValueMap;

import java.net.URI;

@Data
@ToString
@Accessors(chain = true)
public class GatewayLog {

    public static final String CACHE_GATEWAY_CONTEXT = "cacheGatewayContext";
    private long startTime;
    private long endTime;
    /**
     * cache json body
     */
    private Object requestBody;

    private Object userInfo;
    /**
     * cache Response Body
     */
    private StringBuffer responseBody;
    /**
     * request headers
     */
    private HttpHeaders requestHeaders;
    /**
     * cache form data
     */
    private MultiValueMap<String, String> formData;
    /**
     * cache all request data include:form data and query param
     */
    private MultiValueMap<String, String> queryParams;


    private URI requestUrl;

    private String requestIp;

    private HttpMethod requestMethod;
    private HttpStatus responseStatus;
    private HttpHeaders responseHeaders;

    public MediaType getRequestContentType() {
        return this.requestHeaders != null ? this.requestHeaders.getContentType() : null;
    }

    public Long getRequestContentLength() {
        return this.requestHeaders != null ? this.requestHeaders.getContentLength() : null;
    }

    public MediaType getResponseContentType() {
        return this.responseHeaders!=null ? this.responseHeaders.getContentType() :  null;
    }

    public Long getResponseContentLength() {
        return this.responseHeaders!=null ? this.responseHeaders.getContentLength() :  null;
    }


    public GatewayLog setResponseBody(String responseBody) {
        if (this.responseBody == null ){
            this.responseBody = new StringBuffer(responseBody);
        }else{
            this.responseBody.append(responseBody);
        }
        return this;
    }
}